package com.itheima.service.impl;



import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.OrderlistDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderlistService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderlistService.class)
@Transactional
public class OrderlistServiceImpl implements OrderlistService {
    @Autowired
    private OrderlistDao orderlistDao ;
    @Autowired
    private MemberDao memberDao ;
    @Autowired
    private OrderDao orderDao ;
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String startDate = queryPageBean.getStartDate();
        String endDate = queryPageBean.getEndDate();
        String orderType = queryPageBean.getOrderType();
        String orderStatus = queryPageBean.getOrderStatus();
        Page<Map> byPage = orderlistDao.findByPage(startDate,endDate,orderType,orderStatus,queryPageBean.getQueryString());
        return new PageResult(byPage.getTotal(),byPage.getResult());
    }

    @Override
    public Result add(Map<String, String> map, Integer[] setmealId) throws Exception {
        //查询当天是否有预约
        Date orderDate = DateUtils.parseString2Date(map.get("orderDate"));
        OrderSetting orderSetting  = orderSettingDao.findByOrderDate(orderDate);
        if(orderSetting  == null){
            return  new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //查询当天的预约是否满了
        int number = orderSetting .getNumber();
        int reservations = orderSetting .getReservations();
        if (number <= reservations){
            return  new Result(false,MessageConstant.ORDER_FULL);
        }
        //查询是否是会员
        String telephone = map.get("telephone");
        Member findphone = memberDao.findByTelephone(telephone);
        if (findphone == null){
            findphone = new Member();
            findphone.setPhoneNumber(telephone);
            findphone.setIdCard(map.get("idCard"));
            findphone.setSex(map.get("sex"));
            findphone.setRegTime(new Date());
            //将会员信息存入数据库中
            memberDao.add(findphone);
        }

        //4.查询对比会员有没有在同一天预约多次同一个套餐
        //查询数据对比数据
        Order order1 = new Order();
        order1.setMemberId(findphone.getId());
        order1.setSetmealId(setmealId[0]);
        order1.setOrderDate(orderDate);

        List<Order> byOrder1 = orderDao.findByCondition(order1);
        if(byOrder1 != null && byOrder1.size()>0){
            return  new Result(false, MessageConstant.HAS_ORDERED);
        }
        //没有的话就创建预约对象
        Order order = new Order();
        order.setMemberId(findphone.getId());
        order.setSetmealId(setmealId[0]);
        order.setOrderDate(orderDate);
        order.setOrderType(map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        System.out.println(order);
        orderDao.add(order);
        //添加成功后对表中的数据进行修改
        orderSetting .setReservations(reservations + 1);

        orderSettingDao.editReservationsByOrderDate(orderSetting );

        return new Result(true, MessageConstant.ORDER_SUCCESS, orderSetting .getId());
    }

    @Override
    public List<Setmeal> findAll() {
       return  orderlistDao.findAll() ;
    }

    @Override
    public void deleteById(Integer id) {
      orderlistDao.deleteById(id);
    }

    @Override
    public void changeStatus(Map map) {
        Integer id = (Integer) map.get("id");
        String orderStatus = (String) map.get("orderStatus");
        orderlistDao.modifyOrderStatusById(id, orderStatus);
    }

    @Override
    public Map<String,String> findById(Integer id) {

        return  orderlistDao.findById(id);
    }

    @Override
    public Result edit(Map<String,String> map, Integer[] setmealIds) throws Exception {
        String orderDate1 = map.get(map.get("orderDate"));
        Date date = DateUtils.parseString2Date(orderDate1);
        OrderSetting orderSetting  = orderSettingDao.findByOrderDate(date);
        if(orderSetting  == null){
            return  new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //查询当天的预约是否满了
        int number = orderSetting .getNumber();
        int reservations = orderSetting .getReservations();
        if (number <= reservations){
            return  new Result(false,MessageConstant.ORDER_FULL);
        }
        //查询是否是会员
        String telephone = map.get("telephone");
        Member findphone = memberDao.findByTelephone(telephone);
        if (findphone == null){
            findphone = new Member();
            findphone.setPhoneNumber(telephone);
            findphone.setIdCard(map.get("idCard"));
            findphone.setSex(map.get("sex"));
            findphone.setRegTime(new Date());
            //将会员信息存入数据库中
            memberDao.add(findphone);
        }

        //4.查询对比会员有没有在同一天预约多次同一个套餐
        //查询数据对比数据
        Order order1 = new Order();
        order1.setMemberId(findphone.getId());
        order1.setSetmealId(setmealIds[0]);
        order1.setOrderDate(date);

        List<Order> byOrder1 = orderDao.findByCondition(order1);
        if(byOrder1 != null && byOrder1.size()>0){
            return  new Result(false, MessageConstant.HAS_ORDERED);
        }
        //没有的话就创建预约对象
        Order order = new Order();
        order.setMemberId(findphone.getId());
        order.setSetmealId(setmealIds[0]);
        order.setOrderDate(date);
        order.setOrderType(map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        System.out.println(order);
        orderDao.add(order);
        //添加成功后对表中的数据进行修改
        orderSetting .setReservations(reservations + 1);

        orderSettingDao.editReservationsByOrderDate(orderSetting );

        return new Result(true, MessageConstant.ORDER_SUCCESS, orderSetting .getId());
    }


}
